package com.start.manager.controller;


import com.start.manager.domain.common.PowerResult;
import com.start.manager.domain.common.RoleAll;
import com.start.manager.domain.entity.Role;
import com.start.manager.service.IButtonService;
import com.start.manager.service.IRoleButtonService;
import com.start.manager.service.IRoleService;
import com.start.manager.util.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Lijian
 * @since 2020-01-07
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;
    @Autowired
    private IRoleButtonService roleButtonService;

    @Autowired
    private IButtonService buttonService;

    @GetMapping(value = "/getRoles")
    public ResponseResult getRole() {
        List<Role> list = roleService.findRoles();
        List<RoleAll> result = new ArrayList<>();
        for (Role role : list) {
            RoleAll roleAll = new RoleAll();
            BeanUtils.copyProperties(role, roleAll);
            //获取当前的角色 id
            Integer roleId = role.getId();
            Set<Integer> buttonIdList = roleButtonService.findAllByRoleId(roleId);
            //获取角色下的所有button
            List<PowerResult> result1 = buttonService.getResult(buttonIdList);
            List<Map> listchild = new ArrayList<>();
            for (PowerResult powerResult : result1) {
                // 104	1041:add:10001,1041:delete:10003,1041:update:10002,1042:add:10004	1041:商品管理,1042:单位管理	参数管理	/company,/goods
                Map<String, Object> child1 = new HashMap<>();
                List<Map> child2 = new ArrayList<>();
                //把 参数管理 和 104 写进去 一级菜单
                child1.put("id", powerResult.getFistMenuId());
                child1.put("authName", powerResult.getFirstMenuName());
                String[] splitButton = powerResult.getButtons().split(",");
                String firstPath;
                // 只有一级菜单 无二级菜单
                if (StringUtils.isNotBlank((firstPath = powerResult.getFirstMenuPath()))) {
                    child1.put("path", powerResult.getFirstMenuPath());

                    listchild.add(child1);
                } else {
                    String secondMenuName = powerResult.getSecondMenuName();
                    String secondMenuPath = powerResult.getSecondMenuPath();
                    String[] split = secondMenuName.split(",");
                    String[] split1 = secondMenuPath.split(",");
                    for (int i = 0; i < split.length; i++) {
                        Map<String, Object> map = new HashMap<>();
                        map.put("id", split[i].split(":")[0]);
                        map.put("authName", split[i].split(":")[1]);
                        List<Map> listButtonList = new ArrayList<>();
                        for (String s : splitButton) {
                            String[] split2Buuton = s.split(":");
                            if (s.contains(split2Buuton[0])) {
                                // 含有
                                Map<String, Object> buttonMap = new HashMap<>();
                                buttonMap.put("id", split2Buuton[2]);
                                buttonMap.put("authName", split2Buuton[3]);
                                buttonMap.put("buttonCode", split2Buuton[1]);
                                buttonMap.put("path", null);
                                listButtonList.add(buttonMap);
                            }
                        }
                        map.put("children", listButtonList);
                        child2.add(map);
                    }
                    child1.put("children", child2);
                    listchild.add(child1);
                }
            }
            roleAll.setChildren(listchild);
            result.add(roleAll);
        }
        return ResponseResult.success(result);
    }

}
